package com.attendcollatesys.controller.querycontroller;

import com.attendcollatesys.entity.vo.AtMonthTotalsVO;
import com.attendcollatesys.ex.handler.ex.MessageNullValueException;
import com.attendcollatesys.service.ATDayDetailsService;
import com.attendcollatesys.service.ATLeaveExtraDetailService;
import com.attendcollatesys.service.AtMonthTotalsService;
import com.attendcollatesys.service.AtteResultsService;
import com.attendcollatesys.web.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/query")
public class AtteStatisticsController {

    @Autowired
    AtMonthTotalsService atMonthTotalsService;

    @Autowired
    ATDayDetailsService atDayDetailsService;
    @Autowired
    ATLeaveExtraDetailService atLeaveExtraDetailService;

    @Autowired
    AtteResultsService atteResultsService;

    @PostMapping("/monthlyAttendance")
    public ResponseResult<?> monthlyAttendance(@RequestBody Map<String,Object> x) throws MessageNullValueException {
        AtMonthTotalsVO atMonthTotalsVO=null;
        try {
            Integer pageNum = x.get("pageNum") == null?1: (Integer) x.get("pageNum");
            Integer pageSize =  x.get("pageSize") ==null? 10: (Integer) x.get("pageSize");
            Map da = new HashMap();
            if ( x.get("currentMonth") ==null ||"".equals(x.get("currentMonth"))){
                return  new ResponseResult(500,"未输入要查询的月份！");
            }
            da.put("currentMonth",x.get("currentMonth"));
            da.put("name",x.get("name"));
            da.put("rtxNumber",x.get("rtxNumber"));
            atMonthTotalsVO = atMonthTotalsService.queryByDateAndNameAndRtx(pageNum,pageSize,da);
            if ( atMonthTotalsVO.getTotal() ==null ||"0".equals(atMonthTotalsVO.getTotal())){
                return  new ResponseResult(500,"无对应数据！");
            }
        }catch (Exception e){
            return  new ResponseResult(500,e.getMessage());
        }
        return new ResponseResult(200,"查询成功",atMonthTotalsVO);
    }


    @PostMapping("/dailyAttendance")
    public ResponseResult<?> dailyAttendance(@RequestBody Map<String,Object> x) throws MessageNullValueException {
        Map<String,Object> tReturn =null;
        try {
            tReturn = atDayDetailsService.queryATDayDetailsList(x);
            if ( tReturn.get("total") ==null ||"0".equals(tReturn.get("total"))){
                return  new ResponseResult(500,"无对应数据！");
            }
        }catch (Exception e){
            return  new ResponseResult(500,e.getMessage());
        }
        return new ResponseResult(200,"查询成功",tReturn);
    }

    @PostMapping("/overtAndLeave")
    public ResponseResult<?> overtAndLeave(@RequestBody Map<String,Object> x) throws MessageNullValueException {
        Map<String,Object> tReturn =null;
        try {
            tReturn = atLeaveExtraDetailService.selectAllByTaypsExServ(x);
            if ( tReturn.get("total") ==null ||"0".equals(tReturn.get("total"))){
                return  new ResponseResult(500,"无对应数据！");
            }
        }catch (Exception e){
            return  new ResponseResult(500,e.getMessage());
        }
        return new ResponseResult(200,"查询成功",tReturn);
    }

    /**
     * 请假天数排行查询接口
     */
    @PostMapping("/leaveDaysRanking")
    public ResponseResult<?> leaveDaysRanking(@RequestBody Map<String,Object> x) throws MessageNullValueException {
        Map<String,Object> tReturn =null;
        Map<String,Object> tResult = new HashMap<>();
        try {
            tReturn = atLeaveExtraDetailService.selectRankingOfLeaveDays(x);
            tResult.put("leaveDaysSheet",tReturn);
            if ( tReturn.get("dataName") ==null ||((List)tReturn.get("dataName")).isEmpty()){
                return  new ResponseResult(500,"无对应数据！");
            }
        }catch (Exception e){
            return  new ResponseResult(500,e.getMessage());
        }
        return new ResponseResult(200,"查询成功",tResult);
    }

    /**
     * 迟到缺勤查询接口
     */
    @PostMapping("/lateEarlyRanking")
    public ResponseResult<?> lateEarlyRanking(@RequestBody Map<String,Object> x)  {
        Map<String,Object> tReturn = null;
        Map<String,Object> tResult = new HashMap<>();
        try {
            tReturn = atLeaveExtraDetailService.selectRankingOfLateAndAbsent(x);
            tResult.put("lateEarlSheet",tReturn);
            if ( tReturn.get("dataName") ==null ||((List)tReturn.get("dataName")).isEmpty()){
                return  new ResponseResult(500,"无对应数据！");
            }
        }catch (Exception e){
            return  new ResponseResult(500,e.getMessage());
        }
        return new ResponseResult(200,"查询成功",tResult);
    }


    @GetMapping("/oneAtteStata")
    public ResponseResult<?> oneAttStata(String[] monthList ,String rtxNumber){

        Map<Object, Object> result = atteResultsService.selOneAtteCount(rtxNumber, monthList);
        return ResponseResult.okResult(result);

    }






}
